package com.isoft.airportsystem.controller;
import com.isoft.airportsystem.util.PageResult;
import com.isoft.airportsystem.entity.flight.BaggageInfo;
import com.isoft.airportsystem.entity.flight.FlightInfo;
import com.isoft.airportsystem.service.flight.FlightService;
import com.isoft.airportsystem.vo.FlightQueryVO;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.isoft.airportsystem.util.Result;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/flight")
@CrossOrigin(origins = {"http://localhost:8080", "http://localhost:3000", "http://localhost:5173"}, allowCredentials = "true") // 前端运行端口
public class FlightController {

    @Autowired
    private FlightService flightService;

    // 航班查询
    @GetMapping("/list")
    public Result<List<FlightInfo>> queryFlightList(FlightQueryVO queryVO) {
        return Result.success(flightService.queryFlightList(queryVO));
    }

    // 分页查询
    @GetMapping("/page")
    public Result<PageResult<FlightInfo>> queryFlightPage(
            FlightQueryVO queryVO,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return Result.success(flightService.queryFlightPage(queryVO, pageNum, pageSize));
    }

    // 航班详情
    @GetMapping("/detail/{flightId}")
    public Result<FlightInfo> getFlightDetail(@PathVariable String flightId) {
        return Result.success(flightService.getFlightDetail(flightId));
    }

    // 行李信息
    @GetMapping("/baggage/{flightId}")
    public Result<List<BaggageInfo>> getBaggageInfo(@PathVariable String flightId) {
        return Result.success(flightService.getBaggageInfo(flightId));
    }

    // 航班状态统计
    @GetMapping("/stats/status")
    public Result<Map<String, Object>> getFlightStatusStats() {
        return Result.success(flightService.getFlightStatusStats());
    }

    // 航班延误统计
    @GetMapping("/stats/delay")
    public Result<Map<String, Object>> getDelayStats() {
        return Result.success(flightService.getDelayStats());
    }

    // 获取最近航班信息
    @GetMapping("/recent")
    public Result<List<FlightInfo>> getRecentFlights(
            @RequestParam(defaultValue = "10") int limit) {
        return Result.success(flightService.getRecentFlights(limit));
    }

    // 导出航班数据
    @GetMapping("/export")
    public void exportFlightData(FlightQueryVO queryVO, HttpServletResponse response) {
        flightService.exportFlightData(queryVO, response);
    }

    @PostMapping("/delete/{flightId}")
    public Result<?> deleteFlight(@PathVariable String flightId) {
        flightService.deleteFlight(flightId);
        return Result.success();
    }

    @PostMapping("/add")
    public Result<?> addFlight(@RequestBody FlightInfo flightInfo) {
        flightService.addFlight(flightInfo);
        return Result.success();
    }

    @PostMapping("/update")
    public Result<?> updateFlight(@RequestBody FlightInfo flightInfo) {
        flightService.updateFlight(flightInfo);
        return Result.success();
    }
}